package com.xiaocikuai.smallchunks.utils.JWTservice;

import com.xiaocikuai.smallchunks.entity.User.User;
import com.xiaocikuai.smallchunks.mapper.User.UserMapper;
import com.xiaocikuai.smallchunks.config.jwt.JwtUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestHeader;

@Service
public class JwtService {
    @Resource
    private UserMapper userMapper;
    public User deconstruction(@RequestHeader("Authorization") String token){
        String[] parts = token.split(" ");
        if (parts.length == 2 && parts[0].equalsIgnoreCase("Bearer")) {
            String actualToken = parts[1];
            // 从 token 中解析出用户名
            String username = JwtUtils.getClaimsByToken(actualToken).getSubject();
            User info = userMapper.userinfo(username);
            return info;
        } else {
            // Token 不符合预期的格式
            return null;
        }
    }

}
